package com.coconet.gas.abs;



import com.coconet.gas.biz.SysUserBiz;
import com.coconet.gas.dao.SysRoleMapper;
import com.coconet.gas.dao.SysUserRoleMapper;
import com.coconet.gas.vo.dw.SysRole;
import org.springframework.context.MessageSource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public abstract class AbstractBaseController {
    @Resource
    private MessageSource messageSource;
    @Resource
    private SysUserBiz sysUserBiz;
    @Resource
    private SysUserRoleMapper surmapper;
    @Resource
    private SysRoleMapper srmapper;
    /*@Resource
	private DlUserMapper dlUserMapper;
    @Resource
	private DlDeviceMapper dlDeviceMapper;*/


    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }

    public static String getIp() {
        HttpServletRequest request = getRequest();
        return request == null ? "127.0.0.1" : request.getRemoteHost();
    }

    public String getMessage(String keString, String... args) {
        return this.messageSource.getMessage(keString, args, Locale.getDefault());
    }

    public String getAuthorities() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();

        return authorities.toString();

    }

    public List<String> getAuthoritiesList() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
        List<String> li = authorities.stream().map(temp -> temp.toString()).collect(Collectors.toList());
        return li;

    }

    public String getId() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return userDetails.getUsername();

    }

    /*public SysUser getCidAndCname() {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser vo = new SysUser();
        vo = sysUserBiz.selectById(userDetails.getUsername());
        vo.setCname(sysCompanyBiz.selectById(vo.getCid()).getComName());
        return vo;
    }*/

    /*public List<Device> getDevice() {
        List<String> authorities = getAuthoritiesList();
		// TODO Auto-generated method stub
		if (authorities.contains("vadmin")) {
			List<Device> finalDevice = devicemapper.selectAll();
			return finalDevice;
		} else {
			List<Device> deviceLi = new ArrayList<>();
			Example exampleRole = new Example(SysRole.class);
			exampleRole.createCriteria().andIn("roleName", authorities);
			List<SysRole> srList = srmapper.selectByExample(exampleRole);
			// 角色id集合
			List<String> rid = srList.stream().map(n -> n.getId()).collect(Collectors.toList());
			Example exampleRoleDevice = new Example(SysRoleDevice.class);
			exampleRoleDevice.createCriteria().andIn("roleId", rid);
			List<SysRoleDevice> srdLi = sysRoleDeviceMapper.selectByExample(exampleRoleDevice);
			// did 集合
			if (srdLi.size() > 0) {
				List<Integer> didLi = srdLi.stream().map(n -> n.getDid()).collect(Collectors.toList());
				Example exampleDevice = new Example(Device.class);
				exampleDevice.createCriteria().andIn("id", didLi);
				deviceLi.addAll(devicemapper.selectByExample(exampleDevice));

			}

			// 查询自己添加的设备
			Device de = new Device();
			de.setUid(getId());
			List<Device> ownerLi = devicemapper.select(de);
			if (ownerLi.size() > 0) {
				deviceLi.addAll(ownerLi);
			}

			return deviceLi;

		}

        if (authorities.contains("vadmin") || authorities.contains("1")) {
            List<Device> finalDevice = new ArrayList<>();
            finalDevice = devicemapper.selectAll();
            return finalDevice;

        } else {
           *//* UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<Integer> didLi = new ArrayList<>();
            Device de = new Device();
            de.setUid(userDetails.getUsername());
            List<Device> ownerLi = devicemapper.select(de);
            if (ownerLi.size() > 0 && ownerLi != null) {
                didLi.addAll(ownerLi.stream().map(n -> n.getId()).collect(Collectors.toList()));
            }

            List<Device> deviceLi = new ArrayList<>();
            List<Device> finalDevice = new ArrayList<>();

            deviceLi = devicemapper.selectAll();
            DlUser dlUser = new DlUser();
            dlUser.setUserName(userDetails.getUsername());
            List<DlUser> dlUserList = dlUserMapper.select(dlUser);
            for (DlUser dlUser1 : dlUserList) {
                DlDevice dlDevice = new DlDevice();
                dlDevice.setDlId(dlUser1.getDlId());
                List<DlDevice> dlDeviceList = dlDeviceMapper.select(dlDevice);
                for (DlDevice dlDevice1 : dlDeviceList) {
                    for (Device device : deviceLi) {
                        if (device.getId() == dlDevice1.getDeviceId()) {
                            if (!finalDevice.contains(device)) {
                                finalDevice.add(device);
                            }
                        }
                    }
                }
            }

            return finalDevice;*//*
           return null;
        }

    }*/

}
